package com.cosmos.mapdemo;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.tianditu.android.maps.GeoPoint;
import com.tianditu.android.maps.MapController;
import com.tianditu.android.maps.MapView;
import com.tianditu.android.maps.MyLocationOverlay;
import com.tianditu.android.maps.Overlay;

import java.util.List;

/**
 * Created by Leonardo on 2015/7/13.
 */
public class MainActivity extends ActionBarActivity {

    private MapView mapView = null;
    private MyLocationOverlay myLocationOverlay = null;
    private DemoMultipleItemOverlay targetOverlay = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mapView = (MapView) findViewById(R.id.main_mapView);
        setupMapView();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        switch (id) {
            case R.id.action_settings:
                return true;
            case R.id.action_my_location:
                markMyLocation();
                return true;
            case R.id.action_mark_target:
                markMultipleTarget();
                return true;
            case R.id.action_poi_search:
                Intent intent = new Intent(this, PoiSearchActivity.class);
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void setupMapView() {
        mapView.setBuiltInZoomControls(true);
        MapController mapController = mapView.getController();
        GeoPoint point = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
        mapController.setCenter(point);
        mapController.setZoom(12);

        //mapView.setDoubleTapEnable(false);

        mapView.getOverlays().add(new UserOverlay(this));
    }

    private void markMyLocation() {
        if (myLocationOverlay == null) {
            myLocationOverlay = new MyLocationOverlay(this, mapView);
            myLocationOverlay.enableCompass();
            myLocationOverlay.enableMyLocation();
        }

        List<Overlay> overlays = mapView.getOverlays();
        if (!overlays.contains(myLocationOverlay)) {
            overlays.add(myLocationOverlay);
        } else {
            overlays.remove(myLocationOverlay);
        }
    }

    private void markMultipleTarget() {
        if (targetOverlay == null) {
            Resources resources = getResources();
            Drawable marker = resources.getDrawable(R.drawable.tips_arrow);
            targetOverlay = new DemoMultipleItemOverlay(marker, this);
        }

        List<Overlay> overlays = mapView.getOverlays();
        if (!overlays.contains(targetOverlay)) {
            overlays.add(targetOverlay);
        } else {
            overlays.remove(targetOverlay);
        }
    }
}
